//
//  VisitorView.swift
//  swift-微博
//
//  Created by chenrin on 2016/12/14.
//  Copyright © 2016年 zhuoheng. All rights reserved.
//

import UIKit

//定义协议
protocol VisitorViewDelegate : class{
    func visitorViewLogin()
    func visitorViewRegister()
}

private let width : CGFloat = UIScreen.main.bounds.size.width

private let height : CGFloat = UIScreen.main.bounds.size.height

class VisitorView: UIView {

    weak var visitorViewDelegate : VisitorViewDelegate?
    
    ///转盘
   private lazy var iconView : UIImageView = {
       let iv = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
       iv.center = CGPoint(x: width / 2, y: height / 2)
        return iv
    }()
    ///图标
   private lazy var homeIcon : UIImageView = {
        let iv = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
       iv.center = CGPoint(x: width / 2, y: height / 2)
        return iv
    }()
    ///文本
   private lazy var messageLabel: UILabel = {[unowned self] in
        let message = UILabel()
            message.text = "大数据反馈撒地方记录开始大幅"
     let y = self.iconView.frame.maxY
    message.frame = CGRect(x:0, y:y ,width:width,height:20)
    message.textAlignment = .center
    message.font = UIFont.systemFont(ofSize: 16)
    message.textColor = UIColor.darkGray
        return message
    }()
    ///登录
   private lazy var loginButton: UIButton = {
        let btn = UIButton()
        btn.setTitle("登录", for: .normal)
        btn.setTitleColor(UIColor.orange, for: .normal)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 16)
        btn.setBackgroundImage(UIImage(named:"common_button_white_disable"), for: .normal)
        let y = self.messageLabel.frame.maxY
    
        btn.frame = CGRect(x: width / 2 - 90, y: y + 10, width: 70, height: 30)
    btn.addTarget(self, action: #selector(loginButton(button:)), for: .touchUpInside)
        return btn
    }()
    ///注册
   private lazy var registerButton: UIButton = {
        let btn = UIButton()
        btn.setTitle("注册", for: .normal)
        btn.setTitleColor(UIColor.darkGray, for: .normal)
    btn.titleLabel?.font = UIFont.systemFont(ofSize: 16)
        btn.setBackgroundImage(UIImage(named:"common_button_white_disable"), for: .normal)
    let y = self.messageLabel.frame.maxY
    btn.addTarget(self, action: #selector(registButton(button:)), for: .touchUpInside)
    btn.frame = CGRect(x: width / 2 + 20, y: y + 10, width: 70, height: 30)
        return btn
    }()
    ///蒙版图片
    private lazy var maskImageView : UIImageView = {
        let iv = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        let height = self.iconView.frame.size.height / 2
        iv.bounds = CGRect(x: 0, y: 0, width: self.iconView.frame.size.width, height: height-50)
        iv.center = CGPoint(x: width / 2, y: self.iconView.frame.midY)
        iv.contentMode = UIViewContentMode.scaleAspectFill
        return iv
    }()
    
   private override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.white
        addSubview(iconView)
        //addSubview(maskImageView)
        addSubview(homeIcon)
        addSubview(messageLabel)
        addSubview(loginButton)
        addSubview(registerButton)
    

    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupVisitorInfo(isHome:Bool,imageName:String,message:String){
        iconView.isHidden = !isHome
        homeIcon.image = UIImage(named: imageName)
        messageLabel.text = message
        if isHome {
            setupAnmiation()
        }
    }
    private func setupAnmiation(){
        //1.创建动画
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        //2.设置动画属性
        anim.toValue = 2 * M_PI
        anim.duration = 20
        anim.repeatCount = MAXFLOAT
        //代表动画执行完毕久移除
        anim.isRemovedOnCompletion = false
        //3.将动画添加到图层中
        iconView.layer.add(anim, forKey: nil)
    }
    ///注册
    func registButton(button:UIButton) {
        visitorViewDelegate?.visitorViewRegister()
        let accout = OAuthModel.getAccount()
        print(accout?.access_token ?? "")
        
    }
    ///登录
    func loginButton(button:UIButton) {
        visitorViewDelegate?.visitorViewLogin()
    }
}
